package com.jzo2o.foundations.service.impl;

import co.elastic.clients.elasticsearch._types.SortOptions;
import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.es.core.ElasticSearchTemplate;
import com.jzo2o.es.utils.SearchResponseUtils;
import com.jzo2o.foundations.model.domain.ServeAggregation;
import com.jzo2o.foundations.model.dto.response.ServeAggregationSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeSimpleResDTO;
import com.jzo2o.foundations.service.ServeAggregationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.naming.directory.SearchResult;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Mr.M
 * @version 1.0
 * @description 服务搜索
 * @date 2023/12/11 16:47
 */
@Service
@Slf4j
public class ServeAggregationServiceImpl implements ServeAggregationService {

    @Resource
    private ElasticSearchTemplate elasticSearchTemplate;




    @Override
    public List<ServeSimpleResDTO> findServeList(String cityCode, Long serveTypeId, String keyword) {

        SearchRequest.Builder builder = new SearchRequest.Builder();
        //拼装查询条件

        builder.query(query->query.bool(bool->{

            //根据city_code查询
            bool.must(must->
                    must.term(term->term.field("city_code").value(cityCode)));
            //todo 根据服务类型查询

            //根据keyword查询
            if(ObjectUtils.isNotEmpty(keyword)){
                //拼接条件
                bool.must(must->
                        must.multiMatch(multiMatch->
                                multiMatch.query(keyword).fields("serve_item_name","serve_type_name")));

            }

            return bool;

                }));


         List<SortOptions> sortOptionList = new ArrayList<>();
         //添加一个排序字段
        sortOptionList.add(SortOptions.of(sortOptions->sortOptions.field(field->field.field("serve_item_sort_num").order(SortOrder.Asc))));
        //添加排序
        builder.sort(sortOptionList);
        //指定要搜索的索引
        builder.index("serve_aggregation");
        //生成searchRequest
        SearchRequest searchRequest = builder.build();

        //请求es搜索

        SearchResponse<ServeAggregation> search = elasticSearchTemplate.opsForDoc().search(searchRequest, ServeAggregation.class);
        //如果搜索成功
        if(SearchResponseUtils.isSuccess(search)){
            List<ServeAggregation> collect = search.hits().hits().stream().map(item -> {
                ServeAggregation source = item.source();
                return source;
            }).collect(Collectors.toList());

            //将结果集返回
            //将collect转成List<ServeSimpleResDTO> 类型
            List<ServeSimpleResDTO> serveSimpleResDTOS = BeanUtils.copyToList(collect, ServeSimpleResDTO.class);


            return serveSimpleResDTOS;
        }

        return Collections.emptyList();

    }
}
